$.ajax({
	url: "/api/complex/MyNote",
	type: "get",
	data: {
		sid: App.get("loginer.sid")
	},
	success: function(result) {
		console.log(result);
		App.set('$Cache_Note_get_types', result);
	},
	error: function(e) {
		console.log(e);
	}
});

getNotes();
App.set("$Event.get_all_notes", getNotes);

function getNotes() {
	$.ajax({
		url: "/api/complex/MyNote",
		type: "get",
		data: {
			sid: App.get("loginer.sid"),
			datatype: true
		},
		success: function(result) {
			console.log(result);
			$(window).scrollTop(0);
			$.each(result, function(r) {
				// console.log(this.createtime)
				var time = new Date(parseInt(this.createtime));
				// console.log(time);
				this.createtime = time.toLocaleDateString();
			})
			App.set("$Cache_all_notes", result);
		},
		error: function(e) {
			console.log(e);
		}
	});
}



App.set("$Event.get_type_note", function(e, vi) {
	if (vi.get("num")) {

		$.ajax({
			url: "/api/complex/MyNote",
			type: "get",
			data: {
				typeid: vi.get("id"),
				datatype: true
			},
			success: function(result) {
				console.log(result);
				$.each(result, function(r) {
					console.log(this.createtime)
					var time = new Date(parseInt(this.createtime));
					console.log(time);
					this.createtime = time.toLocaleDateString();
				})
				App.set("$Cache_all_notes", result);
			},
			error: function(e) {
				console.log(e);
			}
		});
	} else {
		alert("此分类下没有任何笔记。。。");
	};
})

function getMynote(obj) {
	console.log("hehe");
}

App.set("$Event.add_new_note_type", function() {
	var name = App.get("$Cache_Note_type_new");
	if (name) {
		$.ajax({
			url: "/api/complex/MyNote",
			type: "post",
			data: data = {
				datatype: 1,
				sid: App.get("loginer.sid"),
				id: new Date().getTime(),
				name: name,
				num: 0
			},
			success: function(r) {
				App.push("$Cache_Note_get_types", data);
				// var str = "<div data=" + data.id + " class='item' onclick='getMynote(this)'>" + data.name + "(" + data.num + ")</div>";
				// $(".typebox").append(str);
				// var str2 = "<option value=" + data.id + ">" + data.name + "</option>";
				// $("[name='chosebox']").append(str2);
				myConfirm("添加分类成功！！！");
				$("input").val("");
				// console.log(r);
			},
			error: function(e) {
				console.log(e);
			}
		})
	} else {
		myConfirm("请先输入分类名称！！！");
	}
});


var savedir;
App.set("$Event.create_new_note", function() {
	if (App.get('$Cache_new_note_text')) {
		var src = App.get("savedir");
		var data = {};
		$.ajax({
			url: "/api/complex/MyNote",
			type: "post",
			data: data = {
				id: new Date().getTime(),
				sid: App.get("loginer.sid"),
				typeid: $("[name='chosebox']").val(),
				title: "",
				text: App.get('$Cache_new_note_text'),
				url: "",
				src: src,
				typename: $("[name='chosebox']").find("option:selected").text()
			},
			success: function(r) {
				// console.log(r);
				$.ajax({
					url: "/api/complex/MyNote",
					type: "put",
					data: {
						id: data.typeid
					},
					success: function(result) {
						// console.log(result);
						$("textarea").val("");
						$(".imgset").removeClass("focus");
						savedir = "";
						myConfirm("笔记已添加！！！", function() {
							location.reload();
						}, function() {
							location.reload();
						});
					},
					error: function(error) {
						console.log(error);
					}
				})
			},
			error: function(e) {
				console.log(e);
			}
		})
	} else {
		myConfirm("请填写好您的笔记！！！");
	}
});
// 上传图片
function upfile(obj) {
	var savedir = fileupload(obj);
	App.set("savedir", savedir);
	console.log(savedir)
	if (savedir) {
		$(".imgset").addClass("focus");
		$("#upimg")[0].src = savedir;
	};
}

App.set("$Event.shoe_type_box", function() {
	App.model.toggle("$Cache_istype");
})